package com.six.flowerShopper.controller

import com.github.pagehelper.PageHelper
import com.github.pagehelper.PageInfo
import com.six.flowerShopper.entity.PurchaseHistory
import com.six.flowerShopper.mapper.PurchaseHistoryMapper
import com.six.flowerShopper.mapper.delWrapper
import com.six.flowerShopper.mapper.updateWrapper
import com.six.flowerShopper.mapper.queryByUserId
import com.six.flowerShopper.service.PurchaseHistoryService
import com.six.library.model.ResultData
import com.six.library.model.ResultInfo
import org.apache.commons.lang.StringUtils
import org.springframework.web.bind.annotation.*
import javax.annotation.Resource


/*-*- coding:utf-8 -*-
 * @Author  : debi
 * @Time    : 10/22/22
 * @Software: IntelliJ IDEA
 */
@RestController
@RequestMapping("/purchaseHistory")
@CrossOrigin
class PurchaseHistoryController {
    @Resource
    private lateinit var phm: PurchaseHistoryMapper

    @Resource
    private lateinit var phs: PurchaseHistoryService

    @RequestMapping("/insert")
    fun insert(@RequestBody ph: PurchaseHistory): ResultData {
        val batchIds = phm.insert(ph)
        return ResultInfo.setData(batchIds).setMsg("插入成功")
    }

    @RequestMapping("/insertList")
    fun insertList(@RequestBody flowers: List<PurchaseHistory>): ResultData {
        var batchIds = 0
        flowers.stream().forEach { it ->
            val insert = phm.insert(it)
            batchIds += (insert)
        }
        return ResultInfo.setData(batchIds).setMsg("插入成功")
    }


    /*
    * 直接通过消费记录Id 查询
    * */
    @RequestMapping("/selectById/{id}")
    fun selectById(@PathVariable(value = "id", required = false) id: Int): ResultData {

        val resultInfo = ResultData()
        return try {
            resultInfo.setData(phm.selectById(id))
        } catch (e: Exception) {
            resultInfo.setCode(500).setMsg("查询出错")
        }
    }

    @RequestMapping("/selectByIds")
    fun selectByIds(@RequestBody ids: List<Int>?): ResultData {
        val resultInfo = ResultData()
        try {

            val selectList: List<PurchaseHistory> = if (ids != null && ids.isNotEmpty()) {
                phm.selectBatchIds(ids)
            } else {
                phs.findAll()
            }
            if (selectList.isEmpty()) {
                resultInfo.setCode(300).setMsg("没有数据").setData(selectList)
            } else {
                resultInfo.setCode(200).setMsg("查询成功").setData(selectList)
            }
        } catch (e: Exception) {
            resultInfo.setCode(500).setMsg("查询出错")
        }
        return resultInfo
    }

    @RequestMapping("/delByIds")
    fun delByIds(@RequestBody ids: List<Int>?): ResultData {
        val resultInfo = ResultData()
        try {
            val id: Int = phm.deleteBatchIds(ids)

            return if (id >= 0) {
                resultInfo.setCode(200).setMsg("删除成功").setData(id)
            } else {
                resultInfo.setCode(300).setMsg("删除失败")
            }

        } catch (e: Exception) {
            resultInfo.setCode(500).setMsg("查询出错")
        }
        return resultInfo
    }


    /*
    * 直接通过用户Id 查询
    * */
    @RequestMapping("/selectByUserId/{id}", "/selectByUserId")
    fun selectByUserId(@PathVariable(value = "id", required = false) id: String?): ResultData {
        val resultInfo = ResultData()
        try {
            val selectList: List<PurchaseHistory> = if (id != null && StringUtils.isNotEmpty(id)) {
                phm.selectList(PurchaseHistory.queryByUserId(id.toInt()))
            } else {
                phm.findAll()
            }
            if (selectList.isEmpty()) {
                resultInfo.setCode(300).setMsg("没有数据").setData(selectList)
            } else {
                resultInfo.setCode(300).setMsg("查询成功").setData(selectList)
            }

        } catch (e: Exception) {
            resultInfo.setCode(500).setMsg("查询出错")
        }
        return resultInfo

    }

    @RequestMapping(*["/getpagelist/{pageIndex}/{pageSize}/{userId}", "/getpagelist/{pageIndex}/{pageSize}", "/getpagelist/{pageIndex}", "/getpagelist"])
    fun getPageList(
        @PathVariable(value = "pageIndex", required = false) pageIndex: Int?,
        @PathVariable(value = "pageSize", required = false) pageSize: Int?,
        @PathVariable(value = "userId", required = false) userId: Int?,
    ): ResultData {
        var pageIndex = pageIndex
        var pageSize = pageSize
        var userId = userId
        val result = ResultData()
        //判断pageIndex是否是有效数据
        pageIndex = if (pageIndex == null || pageIndex < 0) 1 else pageIndex
        //判断pageSize是否是有效数据
        pageSize = if (pageSize == null || pageSize < 1) 10 else pageSize
        return try {
            // 分页查询数据
            PageHelper.startPage<PurchaseHistory>(pageIndex, pageSize)
            val list: MutableList<PurchaseHistory> = if (userId != null && userId > 0) {
                phm.selectList(PurchaseHistory.queryByUserId(userId))
            } else {
                phm.findAll()
            }

            val pageInfo = PageInfo(list)
            if (pageInfo.total > 0) {
                result.setMsg("success").setData(pageInfo)
            } else {
                result.setCode(300).setMsg("No Data")
            }
        } catch (e: Exception) {
            result.setCode(500).setMsg("查询数据出错，请与管理员了联系");
        }

    }

    @RequestMapping("/test")
    fun test() {
        println("------------")

    }


    /*
* 直接通过用户Id 删除
* */
    @RequestMapping("/delByUserId/{id}")
    fun delByUserId(@PathVariable(value = "id", required = false) id: Int?): ResultData {
        val resultInfo = ResultData()
        try {
            if (id != null && id > 0) {
                val selectList: Int = phm.deleteByMap(mapOf("id" to id))
                return if (selectList >= 0) {
                    resultInfo.setCode(200).setMsg("删除成功").setData(selectList)
                } else {
                    resultInfo.setCode(300).setMsg("删除失败")
                }
            }
        } catch (e: Exception) {
            resultInfo.setCode(500).setMsg("查询出错")
        }
        return resultInfo

    }

    @RequestMapping("/updateByUserId")
    fun updateByUserId(@RequestBody ph: PurchaseHistory): ResultData {
        try {
            val batchIds = phm.update(ph, PurchaseHistory.updateWrapper(ph))
            if (batchIds >= 0) {
                return ResultInfo.setData(batchIds).setMsg("修改成功")

            }
            return ResultInfo.setCode(300).setData(batchIds).setMsg("修改失败")

        } catch (e: Exception) {
            return ResultInfo.setCode(500).setMsg("修改失败")

        }
    }

    @RequestMapping("/del")
    fun del(@RequestBody ph: PurchaseHistory): ResultData {
        val resultInfo = ResultData()
        try {
            val selectList: Int = phm.delete(PurchaseHistory.delWrapper(ph))
            return if (selectList >= 0) {
                resultInfo.setCode(200).setMsg("删除成功").setData(selectList)
            } else {
                resultInfo.setCode(300).setMsg("删除失败")
            }
        } catch (e: Exception) {
            resultInfo.setCode(500).setMsg("查询出错")
        }
        return resultInfo

    }


}